<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/5/31
 * Time: 下午4:29
 */

use App\Models\Settings;
use Illuminate\Support\HtmlString;

if (!function_exists('setting')){
    /**
     * @param null $name
     * @param string $value
     * @return array|bool|mixed|null|string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    function setting($name=null, $value=''){
        static $_settings;
        if (!is_array($_settings)) $_settings = Settings::fetchWithCache();
        if (is_null($name)) {
            return $_settings;
        }else {
            if ($value === ''){
                return isset($_settings[$name]) ? $_settings[$name] : null;
            }elseif (is_null($value)){
                unset($_settings[$name]);
                return true;
            }else {
                $_settings[$name] = $value;
                return $value;
            }
        }
    }
}


if (!function_exists('substring')){
    /**
     * @param $str
     * @param $length
     * @param string $dot
     * @return string
     */
    function substring($str, $length, $dot='...'){
        if (mb_strlen($str) <= $length) {
            return $str;
        }else {
            return mb_substr($str, 0, $length-strlen($dot)).$dot;
        }
    }
}

/**
 * 格式化距离
 * @param string $distance
 * @return string
 */
function distance($distance){
    if (!$distance) return '';
    if ($distance < 1000){
        return $distance.'m';
    }else {
        return number_format($distance/1000,2).'km';
    }
}

/**
 * 计算两点之间的距离
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return float
 */
function getDistance($lat1,$lng1,$lat2,$lng2){
    $earthRadius = 6377830;
    $lat1 = ($lat1 * pi() ) / 180;
    $lng1 = ($lng1 * pi() ) / 180;

    $lat2 = ($lat2 * pi() ) / 180;
    $lng2 = ($lng2 * pi() ) / 180;

    $calcLongitude = $lng2 - $lng1;
    $calcLatitude  = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);  $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;
    return round($calculatedDistance);
}

/**
 * 16位MD5散列值
 * @param $str
 * @return string
 */
function md5_16($str){
    return substr(md5($str), 0, 16);
}

/**
 * discuz 加减密方法
 * @param string $string
 * @param number $decode
 * @param string $key
 * @param number $expiry
 * @return string
 */
function authcode($string, $decode = 0, $key = '', $expiry = 0) {
    $ckey_length = 4;
    $key  = md5($key ? $key : config('app.key'));
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($decode ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);

    $string = $decode ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);

    $result = '';
    $box = range(0, 255);

    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }

    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }

    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }

    if($decode) {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }
}

/**获取密码密文
 * @param $password
 * @return string
 */
function encrypt_password($password) {
    if (!$password || !is_string($password)) return '';
    return sha1(md5($password));
}

/**
 * 产生一个HASH字符串
 * @return string
 */
function formhash() {
    return md5(substr(time(), 0, -4).config('app.key'));
}

/**
 * 去除一些特殊字符
 * @param string $string
 * @return mixed
 */
function dhtmlspecialchars($string) {
    if(is_array($string)) {
        foreach($string as $key => $val) {
            $string[$key] = dhtmlspecialchars($val);
        }
    } else {
        $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1',
            //$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
            str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
    }
    return $string;
}

/**
 * 生成一个随机字符串
 * @param number $length
 * @param int|number $numeric
 * @return string
 */
function random($length, $numeric = 0) {
    PHP_VERSION < '4.2.0' ? mt_srand((double)microtime() * 1000000) : mt_srand();
    $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
    $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
    $hash = '';
    $max = strlen($seed) - 1;
    for($i = 0; $i < $length; $i++) {
        $hash .= $seed[mt_rand(0, $max)];
    }
    return $hash;
}

/**
 * 去除HTML代码和空格
 * @param string $str
 * @return mixed
 */
function stripHtml($str){
    $str = strip_tags($str);
    $str = str_replace('&amp;', '&', $str);
    $str = str_replace(array('&ldquo;','&rdquo;'),array('“','”'),$str);
    $str = preg_replace('/\s|\n\r|　/', '', $str);
    return $str;
}

/**
 * 打印数组
 * @param mixed $array
 */
function print_array($array){
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}

/**
 * 序列号ID
 * @param mixed $array
 * @return string
 */
function implodeids($array) {
    if(is_array($array) && !empty($array)) {
        return "'".implode("','", is_array($array) ? $array : array($array))."'";
    } else {
        return '';
    }
}

/**
 * 格式化文件尺寸
 * @param number $size
 * @return string
 */
function formatSize($size){
    $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    if ($size == 0) {
        return('n/a');
    } else {
        return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]);
    }
}

/**
 * @param $money
 * @return string
 */
function formatAmount($money) {
    return number_format($money, 2, '.', '');
}

/**
 * 清除文档格式
 */
function cleanUpStyle($str){
    $str = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', "", $str);
    $str = preg_replace('/\s*margin(.*?)pt\s*;/i', "", $str);
    $str = preg_replace('/\s*margin(.*?)cm\s*;/i', "", $str);
    $str = preg_replace('/\s*text-indent:(.*?)\s*;/i', "", $str);
    $str = preg_replace('/\s*line-height:(.*?)\s*;/i', "", $str);
    $str = preg_replace('/\s*page-break-before: [^\s;]+;?"/i', "", $str);
    $str = preg_replace('/\s*font-variant: [^\s;]+;?"/i', "", $str);
    $str = preg_replace('/\s*tab-stops:[^;"]*;?/i', "", $str);
    $str = preg_replace('/\s*tab-stops:[^"]*/i', "", $str);
    $str = preg_replace('/\s*face="[^"]*"/i', "", $str);
    $str = preg_replace('/\s*face=[^ >]*/i', "", $str);
    $str = preg_replace('/\s*font:(.*?);/i', "", $str);
    $str = preg_replace('/\s*font-size:(.*?);/i', "", $str);
    $str = preg_replace('/\s*font-weight:(.*?);/i', "", $str);
    $str = preg_replace('/\s*font-family:[^;"]*;?/i', "", $str);
    $str = preg_replace('/<span style="Times New Roman&quot;">\s\n<\/span>/i', "", $str);
    return $str;
}

/**
 * php生成全球唯一id，php生成随机码，php 生成永不重复字符串
 * @return string
 */
function guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $uuid = substr($charid, 0, 8).
        substr($charid, 8, 4).
        substr($charid,12, 4).
        substr($charid,16, 4).
        substr($charid,20,12);
    return $uuid;
}

/**
 * 选择图片
 * @param $imgFile
 * @return bool
 */
function removeExif($imgFile) {
    if (!function_exists('exif_read_data')) {
        return false;
    }
    $img  = @imagecreatefromjpeg($imgFile);
    if($img === false){
        return false;
    }
    $exif = @exif_read_data($imgFile);
    if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 8:
                $image = imagerotate($img, 90, 0);
                break;
            case 3:
                $image = imagerotate($img, 180, 0);
                break;
            case 6:
                $image = imagerotate($img, -90, 0);
                break;
        }
    }
    imagedestroy($img);
    if (isset($image)) {
        imagejpeg($image, $imgFile);
        imagedestroy($image);
    }
    return true;
}

/**
 * @param array $data
 * @return \Illuminate\Http\JsonResponse
 */
function ajaxReturn($data = []){
    $return = [
        'return_code'=>0,
        'return_msg'=>'SUCCESS'
    ];

    if (is_array($data) && !empty($data)) {
        $return = array_merge($return, $data);
    }
    return response()->json($return);
}

/**
 * @param int $errcode
 * @param string $errmsg
 * @param mixed $data
 * @return \Illuminate\Http\JsonResponse
 */
function ajaxError($errcode, $errmsg, $data = null){
    $return = [
        'errcode'=>$errcode,
        'errmsg'=>$errmsg
    ];
    if (is_array($data) && !empty($data)) {
        $return = array_merge($return, $data);
    }
    return response()->json($return);
}

/**
 * @return HtmlString
 */
function form_verify_field(){
    return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'"><input type="hidden" name="formsubmit" value="yes">');
}

/**
 * @param array $params
 * @return array
 */
function editor_params($params = []){
    $defaults = [
        '_token'=>csrf_token()
    ];

    if (is_array($params)) {
        foreach ($params as $key=>$value){
            $defaults[$key] = $value;
        }
    }
    return $defaults;
}

/**
 * 分页函数
 * @param int $curPage
 * @param int $pageSize
 * @param int $totalCount
 * @param array|string $params
 * @param bool $showTotal
 * @return string
 */
function mutipage($curPage, $pageSize, $totalCount, $params = array(), $showTotal=true){
    $multipage = '<ul class="pagination">';
    $multipage.= $showTotal ? '<li><span>总计'.$totalCount.'条</span></li>' : '';
    $url = url()->current();
    $params = is_array($params) ? http_build_query($params) : $params;
    if ($params) {
        $url = strpos($url, '?') ? $url.'&'.$params : $url.'?'.$params;
    }
    $url = strpos($url, '?') ? $url.'&' : $url.'?';

    $pageCount = $totalCount < $pageSize ? 1 : ceil($totalCount/$pageSize);
    $curPage = min(array($curPage, $pageCount));

    if ($curPage == 1) {
        $multipage.= '<li class="page-item disabled"><span class="page-link">&laquo;</span></li>';
    }else {
        $multipage.= "<li class='page-item'><a href=\"{$url}page=".($curPage-1)."\" class='page-link'>&laquo;</a></li>";
    }

    if ($pageCount < 10) {
        for ($i=1; $i<=$pageCount; $i++){
            if($i == $curPage){
                $multipage.="<li class=\"page-item active\"><span class='page-link'>$i</span></li>";
            }else{
                $multipage.="<li class=\"page-item\"><a href=\"{$url}page=$i\" class=\"page-link\">$i</a></li>";
            }
        }
    }else {
        if ($curPage > 5 && $curPage < $pageCount-4){
            $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=1\" class=\"page-link\">1</a></li>";
            $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=2\" class=\"page-link\">2</a></li>";
            $multipage.= '<li class="page-item disabled"><span class="page-link">...</span></li>';

            $page = $curPage - 2;
            for ($i = 0; $i<5; $i++){
                if($page == $curPage){
                    $multipage.="<li class=\"page-item active\"><span class=\"page-link\">$page</span></li>";
                }else{
                    $multipage.="<li class=\"page-item\"><a href=\"{$url}page=$page\" class=\"page-link\">$page</a></li>";
                }
                $page++;
            }
            $multipage.= '<li class="page-item disabled"><span>...</span></li>';
            $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=".($pageCount-1)."\" class=\"page-link\">".($pageCount-1)."</a></li>";
            $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=".$pageCount."\" class=\"page-link\">".$pageCount."</a></li>";
        }else {
            if ($curPage < 7){
                for ($page=1; $page<7; $page++){
                    if($page == $curPage){
                        $multipage.='<li class="page-item active"><span class="page-link">'.$page.'</span></li>';
                    }else{
                        $multipage.='<li class="page-item"><a href="'.$url.'page='.$page.'" class="page-link">'.$page.'</a></li>';
                    }
                }
            }else {
                $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=1\" class=\"page-link\">1</a></li>";
                $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=2\" class=\"page-link\">2</a></li>";
            }
            $multipage.= '<li class="page-item disabled"><span class="page-link">...</span></li>';

            if ($curPage > ($pageCount-5)){
                for ($page = $pageCount - 5; $page<=$pageCount; $page++){
                    if($page == $curPage){
                        $multipage.="<li class=\"page-item active\"><span class=\"page-link\">$page</span></li>";
                    }else{
                        $multipage.="<li class=\"page-item\"><a href=\"{$url}page=$page\" class=\"page-link\">$page</a></li>";
                    }
                }
            }else {
                $multipage.= "<li><a href=\"{$url}page=".($pageCount-1)."\" class=\"page-link\">".($pageCount-1)."</a></li>";
                $multipage.= "<li><a href=\"{$url}page=".$pageCount."\" class=\"page-link\">".$pageCount."</a></li>";
            }
        }
    }

    if ($curPage < $pageCount){
        $multipage.= "<li class=\"page-item\"><a href=\"{$url}page=".($curPage+1)."\" class=\"page-link\">&raquo;</a></li>";
    }else {
        $multipage.= '<li class="page-item disabled"><span class="page-link">&raquo;</span></li>';
    }

    return   $multipage.'</ul>';
}

/**
 * @param $path
 * @return string
 */
function storage_url($path = '') {
    return config('filesystems.disks.public.url').'/'.$path;
}

/**
 * @param string $path
 * @return string
 */
function storage_public_path($path = ''){
    return config('filesystems.disks.public.root').'/'.$path;
}

/**
 * @param $path
 * @return string
 */
function image_url($path){
    if (preg_match("/([http|https|ftp]\:\/\/)(.*?)/is", $path)){
        return $path;
    }

    if (is_file(storage_public_path($path))) {
        return storage_url($path);
    }else {
        return asset('images/common/nopic.png');
    }
}

/**
 * @param $path
 * @return string
 */
function material_url($path){
    return storage_url($path);
}

/**
 * @param $uid
 * @param string $size
 * @return string
 */
function avatar($uid, $size = 'big'){
    $code = base64_encode(serialize(['uid'=>$uid, 'size'=>$size]));
    return url('avatar/'.$code);
}

/**
 * @param $aid
 * @return string
 */
function post_url($aid) {
    return action('Post\DetailController@index', ['aid'=>$aid]);
}


/**
 * @param $pageid
 * @return string
 */
function page_url($pageid) {
    return action('Pages\DetailController@index', ['pageid'=>$pageid]);
}

/**
 * @param $path
 * @return \Illuminate\Contracts\Routing\UrlGenerator|string
 */
function admin_url($path)
{
    return url('boZbdFeWo/'.$path);
}

/**
 * @param $priv
 * @return bool
 */
function admin_has_priv($priv)
{
    if (auth()->id() == 1000000)
    {
        return true;
    }

    $privileges = session()->get('adminprivileges', []);
    //dd($privileges);
    return in_array($priv, $privileges);
}
